package com.cbgolf.oa.util;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.MyBaseAdapter;
import com.cbgolf.oa.entity.BillUserBean;
import com.cbgolf.oa.entity.PointBean;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/8/26.
 */

public class PopUtil {

  public static PopupWindow pop;

  private static int popWidth;
  private static int popHeight;
  private static List<PointBean> list;
  private static List<String> listNames;

  public interface ItemClickListener {

    void click(int position);
  }

  public static void setPopWidthAndHeight(int popWidth, int popHeight) {
    PopUtil.popWidth = popWidth;
    PopUtil.popHeight = popHeight;
  }

  public static void setStringData(List<String> list) {
    PopUtil.listNames = list;
  }

  public static void setData(List<PointBean> list) {
    PopUtil.list = list;
  }

  public static void showPointFilterList(Activity act, View asView, List<PointBean> list,
      final ItemClickListener itemListener) {
    if (null != pop && pop.isShowing()) {
      pop.dismiss();
      release();
      return;
    }
    pop = new PopupWindow();
    View view = act.getLayoutInflater().inflate(R.layout.pop_list, null);
    View layout = view.findViewById(R.id.pop_list_layout_ll);
    ListView listView = (ListView) view.findViewById(R.id.pop_listview);
    if (Util.listIsNull(list)) {
      release();
      return;
    }
    MyBaseAdapter<PointBean> adapter = new MyBaseAdapter<PointBean>(R.layout.item_text, act, list) {
      @Override
      public View getView(int position, View view, ViewGroup parent, PointBean m_point) {
        setText(R.id.item_text_tv, m_point.name);
        return view;
      }
    };
    listView.setAdapter(adapter);
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    if (popWidth <= 0) {
      DisplayMetrics dm = new DisplayMetrics();
      act.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
      popWidth = dm.widthPixels / 4;
    }
    if (popHeight <= 0) {
      popHeight = ll.WRAP_CONTENT;
    }
    AutoUtil.auto(view);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != pop) {
          pop.dismiss();
        }
        itemListener.click(position);
      }
    });
    pop.setContentView(view);
    pop.setWidth(popWidth);
    pop.setHeight(popHeight);
    pop.setOutsideTouchable(true);
    pop.setBackgroundDrawable(new ColorDrawable(0xf5ffffff));
    pop.setTouchable(true);
    pop.setFocusable(true);
    pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        release();
      }
    });
    pop.showAsDropDown(asView);
  }

  public static void showList(Activity act, View asView, final ItemClickListener itemListener) {
    if (null != pop && pop.isShowing()) {
      pop.dismiss();
      release();
      return;
    }
    pop = new PopupWindow();
    View view = act.getLayoutInflater().inflate(R.layout.pop_list, null);
    View layout = view.findViewById(R.id.pop_list_layout_ll);
    ListView listView = (ListView) view.findViewById(R.id.pop_listview);
    if (Util.listIsNull(listNames)) {
      release();
      return;
    }
    MyBaseAdapter<String> adapter = new MyBaseAdapter<String>(R.layout.item_text, act, listNames) {
      @Override
      public View getView(int position, View view, ViewGroup parent, String s) {
        setText(R.id.item_text_tv, TextUtil.Text(listNames.get(position)));
        return view;
      }
    };
    listView.setAdapter(adapter);
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    if (popWidth <= 0) {
      DisplayMetrics dm = new DisplayMetrics();
      act.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
      popWidth = dm.widthPixels / 4;
    }
    if (popHeight <= 0) {
      popHeight = ll.WRAP_CONTENT;
    }
    AutoUtil.auto(view);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != pop) {
          pop.dismiss();
        }
        if (null != itemListener) {
          itemListener.click(position);
        }
      }
    });
    pop.setContentView(view);
    pop.setWidth(popWidth);
    pop.setHeight(popHeight);
    pop.setOutsideTouchable(true);
    pop.setBackgroundDrawable(new ColorDrawable(0xf5ffffff));
    pop.setTouchable(true);
    pop.setFocusable(true);
    pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        release();
      }
    });
//        try {
//            pop.setElevation(18f);
//        } catch (Exception e) {
//
//        }
    pop.showAsDropDown(asView);
  }

  public static void showUserList(Activity act, View asView, List<BillUserBean> list,
      final ItemClickListener itemListener) {
    if (null != pop && pop.isShowing()) {
      pop.dismiss();
      return;
    }
    pop = new PopupWindow();
    View view = act.getLayoutInflater().inflate(R.layout.pop_list, null);
    View layout = view.findViewById(R.id.pop_list_layout_ll);
    ListView listView = (ListView) view.findViewById(R.id.pop_listview);
    AutoUtil.auto(view);
    MyBaseAdapter<BillUserBean> adapter = new MyBaseAdapter<BillUserBean>(R.layout.item_text, act,
        list) {
      @Override
      public View getView(int position, View view, ViewGroup parent, BillUserBean bean) {
        setText(R.id.item_text_tv, TextUtil.Text(bean.name));
        return view;
      }
    };
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != pop) {
          pop.dismiss();
        }
        if (null != itemListener) {
          itemListener.click(position);
        }
      }
    });
    pop.setContentView(view);
    LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
    popWidth = ScreenUtil.getWidth() / 4;
    popHeight = ll.WRAP_CONTENT;
    pop.setWidth(popWidth);
    pop.setHeight(popHeight);
    pop.setOutsideTouchable(true);
    pop.setBackgroundDrawable(new ColorDrawable(0x00000000));
    pop.setTouchable(true);
    pop.setFocusable(true);
    pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
      @Override
      public void onDismiss() {
        release();
      }
    });
    pop.showAsDropDown(asView);
  }

  public static void release() {
    if (null != pop) {
      if (pop.isShowing()) {
        pop.dismiss();
      }
      pop = null;
    }
    list = null;
    listNames = null;
  }

}
